<!-- 房型管理 -->
<template>
  <div>
    <el-card style="width: 98%; margin: 0 auto; margin-top: 10px">
      <ELTable v-bind="tableConfig" ref="houseTypeTable">
        <template #btns="{ deleteAll }">
          <el-button type="success" @click="add">
            <el-icon class="icon">
              <Plus />
            </el-icon>
            新增房间
          </el-button>
          <el-button type="danger" @click="deleteAll">
            <el-icon class="icon">
              <Delete />
            </el-icon>
            批量删除
          </el-button>
        </template>

        <template #operate="{ scope, deleteHandle }">
          <el-button :icon="Edit" text size="small" class="btn" @click="uploadEdit(scope.row)">
            编辑
          </el-button>
          <el-button
            :icon="Delete"
            text
            size="small"
            class="btn"
            @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </ELTable>

      <!-- 添加 -->
      <houseTypeAdd v-if="dialogVisible" :listParams="listParams" @close="handelClose" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { Edit, Delete } from "@element-plus/icons-vue";
import ELTable, { TableData } from "@/components/ELTable.vue";
import { houseTypeDel, houseTypeDelAll, houseTypeList } from "@/api/config/houseType/houseType";
import { addData } from "@/api/config/houseType/type";
import houseTypeAdd from "@/components/config/houseTypeAdd.vue";

const dialogVisible = ref(false);

let listParams = reactive<addData>({
  id: 0,
  name: "",
});

// 添加
const add = () => {
  listParams = {
    id: 0,
    name: "",
  };
  dialogVisible.value = true;
};

// 编辑
const uploadEdit = (data: addData) => {
  console.log(data);
  listParams = data;
  dialogVisible.value = true;
};

// 关闭弹框
const houseTypeTable = ref();
const handelClose = () => {
  dialogVisible.value = false;
  houseTypeTable.value.getData();
};

const tableConfig = reactive<TableData>({
  loadData: houseTypeList,
  delete: houseTypeDel,
  deleteAll: houseTypeDelAll,
  columns: [
    {
      prop: "id",
      label: "序号",
      attrs: {
        width: "140px",
      },
    },
    {
      prop: "name",
      label: "房间名称",
    },
    {
      prop: "operate",
      label: "操作",
      slot: true,
    },
  ],
});
</script>

<style lang="less" scoped>
.btn {
  width: 40px;
  color: rgb(64, 158, 255);
  border: none;
}

.btn:last-child {
  color: rgb(245, 108, 127);
}
</style>
